/*
 * SPDX-FileCopyrightText: 2023 TU Dresden
 * SPDX-FileCopyrightText: 2024 HAW Hamburg
 * SPDX-License-Identifier: LGPL-2.1-only
 */

/**
 * @ingroup     tests
 * @{
 *
 * @brief       Tests the PSA hash configurations
 *              Contents have been copied from `examples/advanced/psa_crypto`
 *
 * @author      Mikolai Gütschow <mikolai.guetschow@tu-dresden.de>
 * @author      Lena Boeckmann <lena.boeckmann@haw-hamburg.de>
 *
 * @}
 */

#include <stdio.h>
#include <stdint.h>

#include "psa/crypto.h"

static const uint8_t msg[] = "Hello World!";
static const size_t msg_len = sizeof(msg)-1; // exclude NULL-byte

static const uint8_t hash_sha224[] = {
    0x45, 0x75, 0xbb, 0x4e, 0xc1, 0x29, 0xdf, 0x63, 0x80, 0xce, 0xdd, 0xe6, 0xd7,
    0x12, 0x17, 0xfe, 0x05, 0x36, 0xf8, 0xff, 0xc4, 0xe1, 0x8b, 0xca, 0x53, 0x0a,
    0x7a, 0x1b};

static const uint8_t hash_sha256[] = {
    0x7f, 0x83, 0xb1, 0x65, 0x7f, 0xf1, 0xfc, 0x53, 0xb9, 0x2d, 0xc1, 0x81, 0x48,
    0xa1, 0xd6, 0x5d, 0xfc, 0x2d, 0x4b, 0x1f, 0xa3, 0xd6, 0x77, 0x28, 0x4a, 0xdd,
    0xd2, 0x00, 0x12, 0x6d, 0x90, 0x69};

static const uint8_t hash_sha384[] = {
    0xbf, 0xd7, 0x6c, 0x0e, 0xbb, 0xd0, 0x06, 0xfe, 0xe5, 0x83, 0x41, 0x05, 0x47,
    0xc1, 0x88, 0x7b, 0x02, 0x92, 0xbe, 0x76, 0xd5, 0x82, 0xd9, 0x6c, 0x24, 0x2d,
    0x2a, 0x79, 0x27, 0x23, 0xe3, 0xfd, 0x6f, 0xd0, 0x61, 0xf9, 0xd5, 0xcf, 0xd1,
    0x3b, 0x8f, 0x96, 0x13, 0x58, 0xe6, 0xad, 0xba, 0x4a};

static const uint8_t hash_sha512[] = {
    0x86, 0x18, 0x44, 0xd6, 0x70, 0x4e, 0x85, 0x73, 0xfe, 0xc3, 0x4d, 0x96, 0x7e,
    0x20, 0xbc, 0xfe, 0xf3, 0xd4, 0x24, 0xcf, 0x48, 0xbe, 0x04, 0xe6, 0xdc, 0x08,
    0xf2, 0xbd, 0x58, 0xc7, 0x29, 0x74, 0x33, 0x71, 0x01, 0x5e, 0xad, 0x89, 0x1c,
    0xc3, 0xcf, 0x1c, 0x9d, 0x34, 0xb4, 0x92, 0x64, 0xb5, 0x10, 0x75, 0x1b, 0x1f,
    0xf9, 0xe5, 0x37, 0x93, 0x7b, 0xc4, 0x6b, 0x5d, 0x6f, 0xf4, 0xec, 0xc8};

static const uint8_t hash_sha512_224[] = {
    0xba, 0x07, 0x02, 0xdd, 0x8d, 0xd2, 0x32, 0x80, 0xb6, 0x17, 0xef, 0x28, 0x8b,
    0xcc, 0x7e, 0x27, 0x60, 0x60, 0xb8, 0xeb, 0xcd, 0xdf, 0x28, 0xf8, 0xe4, 0x35,
    0x6e, 0xae};

static const uint8_t hash_sha512_256[] = {
    0xf3, 0x71, 0x31, 0x9e, 0xee, 0x6b, 0x39, 0xb0, 0x58, 0xec, 0x26, 0x2d, 0x4e,
    0x72, 0x3a, 0x26, 0x71, 0x0e, 0x46, 0x76, 0x13, 0x01, 0xc8, 0xb5, 0x4c, 0x56,
    0xfa, 0x72, 0x22, 0x67, 0x58, 0x1a};

static const uint8_t msg_long[] = {
    0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
    0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
    0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
    0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
    0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
    0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
    0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
    0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
    0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
    0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
    0xaa
};

static const uint8_t hash_long_sha224[] = {
    0x89, 0xf0, 0xbf, 0x9d, 0x18, 0xe3, 0x68, 0x66, 0xb9, 0x19, 0x8d, 0x9d, 0x1b,
    0xa9, 0x9f, 0x4c, 0xa7, 0xcb, 0x83, 0x9c, 0xf9, 0x7c, 0xd9, 0x71, 0xe3, 0xef,
    0x41, 0x6c
};

static const uint8_t hash_long_sha256[] = {
    0x45, 0xad, 0x4b, 0x37, 0xc6, 0xe2, 0xfc, 0x0a, 0x2c, 0xfc, 0xc1, 0xb5, 0xda,
    0x52, 0x41, 0x32, 0xec, 0x70, 0x76, 0x15, 0xc2, 0xca, 0xe1, 0xdb, 0xbc, 0x43,
    0xc9, 0x7a, 0xa5, 0x21, 0xdb, 0x81
};

static const uint8_t hash_long_sha384[] = {
    0xad, 0x51, 0xdd, 0xb7, 0x80, 0x48, 0x4e, 0xc6, 0xce, 0xb4, 0x96, 0xbc, 0xc1,
    0xe2, 0x4e, 0xf8, 0x23, 0x20, 0xe2, 0xe2, 0x68, 0x7d, 0x6c, 0xba, 0xef, 0x37,
    0xcf, 0x9f, 0x47, 0xc5, 0xa6, 0xfd, 0xda, 0xe5, 0x19, 0xe9, 0x6c, 0x98, 0x6f,
    0x45, 0x97, 0x5d, 0xbc, 0x31, 0xb8, 0x09, 0x91, 0x37
};

static const uint8_t hash_long_sha512[] = {
    0xe1, 0xb5, 0x2c, 0x4f, 0xf8, 0xce, 0x9c, 0x4b, 0x60, 0xbd, 0x8e, 0xc7, 0x85,
    0xab, 0x7b, 0xf3, 0xdf, 0xfc, 0x70, 0x23, 0xf7, 0xc5, 0x15, 0x88, 0xf9, 0x6b,
    0x94, 0xee, 0xba, 0x80, 0xca, 0x3b, 0x9b, 0x9e, 0xd0, 0x5a, 0xb2, 0xac, 0x87,
    0x97, 0xbb, 0x70, 0x39, 0xd6, 0x81, 0xf2, 0xe4, 0x1f, 0xcf, 0xe6, 0xdd, 0xda,
    0xb2, 0xe9, 0x51, 0x22, 0xd9, 0xc7, 0x16, 0xc2, 0xb8, 0x40, 0x6b, 0xd4
};

static const uint8_t hash_long_sha512_224[] = {
    0x6f, 0xcd, 0x48, 0x1d, 0x5c, 0x9a, 0xc8, 0x8d, 0x27, 0x91, 0x91, 0xd0, 0xbf,
    0x19, 0x2d, 0x77, 0xd9, 0x7b, 0x35, 0x64, 0x82, 0x6d, 0xd3, 0xe4, 0xef, 0xb7,
    0xc1, 0xc2
};

static const uint8_t hash_long_sha512_256[] = {
    0x51, 0x28, 0xb1, 0xa1, 0x72, 0x14, 0xe7, 0x4b, 0x76, 0xb5, 0x28, 0x51, 0xab,
    0xc0, 0xec, 0xc0, 0x99, 0x56, 0x32, 0x77, 0xcd, 0x20, 0x78, 0xa7, 0x56, 0x4f,
    0x63, 0x52, 0x68, 0x81, 0x4a, 0xce
};

/**
 * @brief   Example function to use different hash algorithms
 *          with the PSA Crypto API.
 *
 * @return  psa_status_t
 */
psa_status_t example_hash(void)
{
    psa_status_t status = PSA_ERROR_DOES_NOT_EXIST;

    status = psa_hash_compare(PSA_ALG_SHA_224, msg, msg_len, hash_sha224, sizeof(hash_sha224));
    if (status != PSA_SUCCESS) {
        return status;
    }

    status = psa_hash_compare(PSA_ALG_SHA_256, msg, msg_len, hash_sha256, sizeof(hash_sha256));
    if (status != PSA_SUCCESS) {
        return status;
    }

    status = psa_hash_compare(PSA_ALG_SHA_384, msg, msg_len, hash_sha384, sizeof(hash_sha384));
    if (status != PSA_SUCCESS) {
        return status;
    }

    status = psa_hash_compare(PSA_ALG_SHA_512, msg, msg_len, hash_sha512, sizeof(hash_sha512));
    if (status != PSA_SUCCESS) {
        return status;
    }

    status = psa_hash_compare(PSA_ALG_SHA_512_224, msg, msg_len, hash_sha512_224, sizeof(hash_sha512_224));
    if (status != PSA_SUCCESS) {
        return status;
    }

    status = psa_hash_compare(PSA_ALG_SHA_512_256, msg, msg_len, hash_sha512_256, sizeof(hash_sha512_256));
    if (status != PSA_SUCCESS) {
        return status;
    }

    status = psa_hash_compare(PSA_ALG_SHA_224, msg_long, sizeof(msg_long), hash_long_sha224, sizeof(hash_long_sha224));
    if (status != PSA_SUCCESS) {
        return status;
    }

    status = psa_hash_compare(PSA_ALG_SHA_256, msg_long, sizeof(msg_long), hash_long_sha256, sizeof(hash_long_sha256));
    if (status != PSA_SUCCESS) {
        return status;
    }

    status = psa_hash_compare(PSA_ALG_SHA_384, msg_long, sizeof(msg_long), hash_long_sha384, sizeof(hash_long_sha384));
    if (status != PSA_SUCCESS) {
        return status;
    }

    status = psa_hash_compare(PSA_ALG_SHA_512, msg_long, sizeof(msg_long), hash_long_sha512, sizeof(hash_long_sha512));
    if (status != PSA_SUCCESS) {
        return status;
    }

    status = psa_hash_compare(PSA_ALG_SHA_512_224, msg_long, sizeof(msg_long), hash_long_sha512_224, sizeof(hash_long_sha512_224));
    if (status != PSA_SUCCESS) {
        return status;
    }

    status = psa_hash_compare(PSA_ALG_SHA_512_256, msg_long, sizeof(msg_long), hash_long_sha512_256, sizeof(hash_long_sha512_256));
    if (status != PSA_SUCCESS) {
        return status;
    }

    return status;
}
